<template>
  <div class="home"
       ref="home">
    <div class="top-bg"></div>
    <li class="top-title">首页</li>
    <!-- 搜索框 -->
    <van-search shape="round"
                disabled
                background='transparent'
                @click="goRouter('/search')"
                placeholder="寻找更多优质商品" />
    <!-- 分类tab -->
    <div class="class-tab">
      <van-tabs swipe-threshold="9"
                @click="goClass"
                background="transparent">
        <van-tab v-for="item in 8"
                 :key="item"
                 @click="goClass"
                 :title="`标签`"></van-tab>
      </van-tabs>
      <img :src="require('@/assets/image/home/class_icon.png')"
           @click="goClass" />
    </div>
    <!-- banner -->
    <van-swipe class="my-swipe"
               :autoplay="3000"
               indicator-color="white">
      <van-swipe-item>1</van-swipe-item>
      <van-swipe-item>2</van-swipe-item>
      <van-swipe-item>3</van-swipe-item>
      <van-swipe-item>4</van-swipe-item>
    </van-swipe>
    <!-- 菜单 -->
    <van-grid :column-num="4"
              :border="false"
              icon-size="1rem"
              clickable>
      <van-grid-item v-for="value in 4"
                     :key="value"
                     :icon="require('@/assets/image/home/lysj.png')"
                     text="优选超市" />
    </van-grid>
    <!-- 秒杀专区 -->
    <div class="home-title">
      <li class="title-left">
        <img :src="require('@/assets/image/home/zan.png')"
             alt="">
        <span>限时秒杀</span>
      </li>
      <li class="title-right">倒计时：
        <van-count-down :time="seckillTime">
          <template #default="timeData">
            <span class="block">{{ timeData.hours }}</span>
            <span class="colon">:</span>
            <span class="block">{{ timeData.minutes }}</span>
            <span class="colon">:</span>
            <span class="block">{{ timeData.seconds }}</span>
          </template>
        </van-count-down>
      </li>
    </div>
    <div class="seckill">
      <good-show-card v-for="item in 4"
                      @click.native="goRouter('/seckill')"
                      :key="item">
      </good-show-card>
    </div>
    <!-- 积分福利专区 -->
    <div class="home-title">
      <li class="title-left">
        <span>积分福利专区</span>
      </li>
    </div>
    <div class="bonus">
      <li @click="goRouter('/bonus')">
        <span>今日值得买</span>
        <span>买到就是赚到 限量抢</span>
        <span>GO></span>
      </li>
      <div>
        <li @click="goRouter('/bonus')">
          <span>热门话题互动</span>
          <span>内含福利 速来围观</span>
          <span>GO></span>
        </li>
        <li @click="goRouter('/bonus')">
          <span>酒水专场</span>
          <span>超值折扣 每日更新</span>
          <span>GO></span>
        </li>
      </div>
    </div>
    <!-- 爆款热卖 -->
    <div class="home-title">
      <li class="title-left">
        <img :src="require('@/assets/image/home/huo.png')"
             alt="">
        <span>爆款热卖</span>
      </li>
      <li class="title-right">
        <span class="more-span">更多</span>
      </li>
    </div>
    <div class="hot-sale">
      <div v-for="item in 4"
           :key="item"
           @click="goGoodDetail(item)">
        <em>TOP{{ item }}</em>
        <img :src="require('@/assets/image/default_img.png')"
             alt="">
        <span class="text-overflow_2-xzh">可爱卡通狗LED护眼
          灯</span>
        <li>
          <span>¥388</span>
          <span>···</span>
        </li>
      </div>
    </div>
    <!-- 猜你喜欢 -->
    <div class="home-title">
      <li class="title-left">
        <img style="width:0.1rem"
             :src="require('@/assets/image/home/tiao.png')"
             alt="">
        <span>猜你喜欢</span>
      </li>
    </div>
    <div class="home-guess">
      <div class="guess-div"
           v-for="item in 5"
           :key="item"
           @click="goGoodDetail(item)">
        <em>月销99笔</em>
        <img :src="require('@/assets/image/default_img.png')">
        <div>
          <li class="text-overflow_2-xzh">乐事无限薯片三连罐3罐休 闲油炸食品</li>
          <li>
            <span>¥388</span>
            <span>¥388</span>
          </li>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodShowCard from '@/components/GoodShowCard'
import preventBack from '@/mixins/preventBack'
export default {
  name: 'home',
  data () {
    return {
      seckillTime: 12.3 * 60 * 60 * 1000,
      scrollY: 0,
    }
  },
  components: {
    GoodShowCard
  },
  mixins: [preventBack],
  //记录离开时的位置
  beforeRouteLeave (to, from, next) {
    //保存滚动条元素div的scrollTop值
    this.scrollY = this.$refs.home.scrollTop
    next()
  },
  // 为div元素重新设置保存的scrollTop值
  beforeRouteEnter (to, from, next) {
    next(vm => {  // vm = this
      vm.$nextTick(() => {
        vm.$refs.home.scrollTop = vm.scrollY
      })
    })
  },
  created () {
  },
  methods: {
    goRouter (path) {
      this.$router.push({ path })
    },
    goGoodDetail (id) {
      this.$router.push({ path: '/goodsDetail', query: { id } })
    },
    goClass () {
      this.$router.push({ path: '/class' })
    },
  }
}
</script>
<style lang="scss" scoped>
.home {
  position: relative;
  width: 100%;
  box-sizing: border-box;
  padding: 0 0.25rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  .top-bg {
    width: 100%;
    height: 4.64rem;
    @include bgImgCover('../../assets/image/home/top_bg.png');
    position: absolute;
    top: 0;
  }
  .top-title {
    font-size: 0.36rem;
    color: #ffffff;
    margin-top: 0.55rem;
    z-index: 1;
  }
  ::v-deep .van-field__left-icon .van-icon {
    display: block;
    font-size: 22px;
    line-height: inherit;
  }
  .van-search {
    width: 100%;
    z-index: 1;
    margin-top: 0.3rem;
  }
  .class-tab {
    height: 0.6rem;
    width: 100%;
    display: flex;
    z-index: 1;
    .van-tabs {
      flex: 1;
      ::v-deep .van-tabs__line {
        background: linear-gradient(
          90deg,
          #fff 0%,
          rgba(255, 255, 255, 0) 100%
        );
        width: 0.6rem;
      }
      ::v-deep .van-tab {
        color: #fff;
      }
    }
    ::v-deep .van-tabs--line .van-tabs__wrap {
      height: 100%;
    }
    img {
      width: 0.35rem;
      height: 0.35rem;
      margin-top: 0.12rem;
    }
  }
  .my-swipe {
    width: 100%;
    height: 3rem;
    margin-top: 0.2rem;
    border-radius: 0.2rem;
    overflow: hidden;
    background-color: pink;
  }
  .van-grid {
    width: 100%;
    height: 1.8rem;
    ::v-deep .van-grid-item__text {
      font-size: 0.28rem;
    }
  }
  .home-title {
    height: 1rem;
    width: 100%;
    margin-top: 0.1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    li {
      display: flex;
      align-items: center;
    }
    .title-left {
      font-size: 0.32rem;
      color: #333333;
      font-weight: 600;
      img {
        width: 0.33rem;
        height: 0.33rem;
        margin-right: 0.2rem;
      }
    }
    .title-right {
      font-size: 0.26rem;
      color: #333333;
      .van-count-down {
        .colon {
          display: inline-block;
          margin: 0 4px;
          color: #999999;
        }
        .block {
          display: inline-flex;
          justify-content: center;
          align-items: center;
          width: 0.35rem;
          height: 0.35rem;
          color: #e95f31;
          font-size: 0.24rem;
          background-color: #eeeeee;
          border-radius: 0.05rem;
        }
      }
      .more-span {
        background-color: #ffecdd;
        width: 1.2rem;
        height: 0.45rem;
        font-size: 0.28rem;
        color: #e85014;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 0.5rem;
      }
    }
  }
  .seckill {
    display: flex;
  }
  .bonus {
    width: 100%;
    height: 3.7rem;
    display: flex;
    li {
      width: 2.9rem;
      height: 100%;
      margin-right: 0.1rem;
      background-color: #ecf7fd;
      border-radius: 0.1rem;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      padding: 0.3rem 0 0 0.3rem;
      line-height: 0.4rem;
      span {
        &:first-child {
          font-size: 0.3rem;
          color: #000000;
        }
        &:nth-child(2) {
          font-size: 0.25rem;
          color: #727272;
        }
        &:last-child {
          width: 0.7rem;
          height: 0.25rem;
          line-height: 0.28rem;
          text-align: center;
          background-color: #ff0e15;
          color: #ffffff;
          border-radius: 0.15rem;
        }
      }
    }
    div {
      flex: 1;
      display: flex;
      flex-direction: column;
      li {
        flex: 1;
        width: 100%;
        border-radius: 0.1rem;
        &:first-child {
          background-color: #fff799;
          margin-bottom: 0.1rem;
        }
        &:last-child {
          background-color: #fcedf4;
        }
      }
    }
  }
  .hot-sale {
    height: 3.7rem;
    width: 100%;
    display: flex;
    overflow-x: auto;
    div {
      flex-shrink: 0;
      width: 2.5rem;
      margin-right: 0.1rem;
      background-color: #feecdb;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      padding: 0.2rem;
      position: relative;
      em {
        position: absolute;
        top: 0.2rem;
        left: 0.2rem;
        width: 1rem;
        height: 0.38rem;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #fff;
        background-color: #ff0e15;
        border-radius: 0.1rem 0.2rem 0.2rem 0;
      }
      img {
        width: 100%;
        height: 2.1rem;
      }
      span {
        font-size: 0.24rem;
      }
      li {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        span {
          font-size: 0.3rem;
          color: #ff0e15;
          &:last-child {
            font-weight: 700;
          }
        }
      }
    }
  }
  .home-guess {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .guess-div {
      width: 3.3rem;
      height: 4.4rem;
      box-shadow: 1px 1px 3px rgba(1, 1, 1, 0.1);
      position: relative;
      margin-bottom: 0.2rem;
      border-radius: 0.1rem;
      em {
        position: absolute;
        bottom: 0.15rem;
        right: 0.25rem;
        font-size: 0.18rem;
        color: #666666;
      }
      img {
        width: 100%;
        height: 60%;
      }
      div {
        height: 40%;
        width: 100%;
        box-sizing: border-box;
        padding: 0.3rem 0.2rem;
        li {
          &:first-child {
            font-size: 0.24rem;
            margin-bottom: 0.1rem;
          }
          &:last-child {
            width: 100%;
            span {
              &:first-child {
                font-size: 0.32rem;
                color: #e85014;
              }
              &:last-child {
                font-size: 0.28rem;
                text-decoration: line-through;
                color: #999999;
              }
            }
          }
        }
      }
    }
  }
}
</style>